<?php

if( !defined( 'MEDIAWIKI' ) )
die(-1);

require_once('DatabaseFunctions.php');


// Set up the new special page
$dir = dirname( __FILE__ ) . '/';
//$wgExtensionMessagesFiles['UserTimeline'] = $dir . 'UserTimeline.i18n.php';
//$wgAutoloadClasses['UserUpdateStatus'] = $dir . 'UserUpdateStatus.php';

#install extension hook
$wgExtensionFunctions[] = "wfUserUpdateStatus";
$wgAjaxExportList[] = 'wfAjaxUpdateStatus';

function wfAjaxUpdateStatus($status){
	global $wgUser;
	$db=wfGetDB( DB_MASTER );
	$sql="insert into ss_user_update_status (idUser, statusText, date) values(".$wgUser->mId.",'$status',now());";
	$db->begin();
	if ($db->query($sql)){
		$db->commit();
		return $status;
	}
}


function wfUserUpdateStatus(){
  global $wgRequest, $wgDBprefix, $wgOut, $wgUser, $IP, $wgUserLevels;
  $value = $wgRequest->getText( 'title' );
  $values = explode(":", $value);
  $user=$values[count($values)-1];
  
  $db=wfGetDB( DB_MASTER );
  
  $currentUser = User::newFromName( $user );
  
  
  $sql="select statusText status from ss_user_update_status where idUser=".$currentUser->getId()." order by date desc limit 1;";
  $res = $db->query($sql);
  if($row = $db->fetchObject($res))
	$status=$row->status;

  //First check if the type of content (url) is the kind to render this module (Main_Page)
  if ($values[0] == 'User') {
	 $out="<div id='show_status_user'>$status</div>";

	 if( $wgUser->isLoggedIn() && $user==$wgUser->mName){
		$wgOut->addScript("<script type=\"text/javascript\" src=\"/skins/common/updateStatus.js\"></script>\n");
		
		$out .= '<div class="user_update_status">';
		$out .= '<form method="post" id="status_update_form" class="status-update-form" action=""><div style="margin: 0pt; padding: 0pt;"></div>

	  
		
		<div class="info">
			<input type="input" accesskey="u" name="input_status" id="input_status">
			<div class="status-btn">
			  <input type="button" id="update-submit" value="actualizar" name="update"  onclick="sajax_do_call(\'wfAjaxUpdateStatus\',[document.getElementById(\'input_status\').value],cbUpdateStatus);">
			</div>


		</div>
	  
	</form>';
	   
		
		$out .= '</div>';
	}


    $wgOut->switchBocataMode(true);
    $wgOut->setOrder(-80);
    $wgOut->addHtml($out);
    $wgOut->resetOrder();
  }



}




